#!/bin/bash

# set -e

. $ZMICRO_HOME/core/env

help() {
  echo "Usage:"
  echo "  zmicro plugin fn <plugin_name> <fn_name> <fn_args...>"
}

core() {
  if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
    help
    exit 0
  fi

  local plugin_name=$1
  local fn=$2
  local args=${@:3}

  if [ -z "$plugin_name" ]; then
    log::error "[plugin][fn] plugin_name is required"
    exit 1
  fi

  if [ -z "$fn" ]; then
    log::error "[plugin][fn] fn_name is required"
    exit 1
  fi

  plugin::load_core $plugin_name

  type $fn >>/dev/null 2>&1
  if [ $? -eq 0 ]; then
    eval "$fn $args"
  else
    log::error "$fn method is not not found"
  fi
}

run() {
  core $@
}

run $@
